package org.luckyjourney.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.luckyjourney.entity.Comment;
import org.luckyjourney.entity.agent.Couselor;
import org.luckyjourney.entity.user.User;
import org.luckyjourney.mapper.CommentMapper;
import org.luckyjourney.mapper.agent.CouselorMapper;
import org.luckyjourney.mapper.user.UserMapper;
import org.luckyjourney.service.CommentService;
import org.luckyjourney.service.consultants.ConsultantsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author 飒飒
 * @ClassName CommentServiceImpl
 * @description: TODO
 * @date 2024年03月08日
 * @version: 1.0
 */
@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CouselorMapper couselorMapper;


    @Override
    public List<Comment> pageComment(Long videoId) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getVideoId, videoId);
        //查询文章id的评论
        List<Comment> comments = commentMapper.selectList(wrapper);
        //获取评论人的nickName
        comments.forEach(comment -> {
            String userId = comment.getUserId();
            if (userId.substring(0,1).equals("a")){
               Long couselorId= Long.valueOf(userId.substring(1));
                //咨询师
                Couselor couselor = couselorMapper.selectById(couselorId);
                if (!ObjectUtils.isEmpty(couselor)){
                    comment.setNickName(couselor.getNickName());
                    comment.setAvatar(couselor.getAvatar());
                    commentMapper.updateComment(comment);
                }
            }else {
                Long regularUsersId= Long.valueOf(userId.substring(1));
                User regularUsers = userMapper.selectById(regularUsersId);
                if (!ObjectUtils.isEmpty(regularUsers)){
                    comment.setNickName(regularUsers.getNickName());
                    comment.setAvatar(regularUsers.getAvatar());
                    commentMapper.updateComment(comment);
                }
            }

        });
        //查询pid为空的评论
        List<Comment> originList  = comments.stream().filter(comment -> comment.getParentId() == null).collect(Collectors.toList());
        originList.forEach(comment -> {
//            过滤得到回复的父id等于评论的id
            List<Comment> commentList = comments.stream().filter(c -> comment.getId().equals(c.getParentId())).collect(Collectors.toList());
            commentList.forEach(c -> {
                if (c.getParentCommentId() != null){
                    Comment comment1 = commentMapper.selectById(c.getParentCommentId());
                    c.setParentNickName(comment1.getNickName());
                }
            });
            /*commentList.forEach(c -> {
//                如果存在回复的父id，给回复设置其父评论的用户id和用户昵称，这样评论就有能@的人的用户id和昵称
//                v相当于过滤得到的父评论对象
                Optional<Comment> pComment = comments.stream().filter(c1 -> c1.getId().equals(c.getParentId())).findFirst();
                pComment.ifPresent((v ->{
//                                找到父级评论的用户id和用户昵称，并设置当前的回复对象
                    c.setParentNickName(v.getNickName());
                }));
            });*/
            comment.setChildren(commentList);
        });
        //将originList倒叙排序
        originList.sort((o1, o2) -> o2.getCreateTime().compareTo(o1.getCreateTime()));
        return originList;
    }


    @Override
    public String remove(Long id) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<Comment>();
        wrapper.eq(Comment::getId,id);
        List<Comment> comments = commentMapper.selectList(wrapper);
        if (comments.size()<0){
            return "没有找到改评论";
        }
        remove(wrapper);
        return "删除成功";
    }
}
